package com.ctg.payroll2.action.social;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.common.ColumnModel;
import com.ctg.payroll2.bean.filter.social.CsEmpsocialResultFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.social.CsEmpsocialResult;
import com.ctg.payroll2.bean.po.social.CsEmpsocialTotal;
import com.ctg.payroll2.service.social.CsEmpsocialResultService;
import com.ctg.payroll2.util.Constants;
import com.ctg.payroll2.util.xlsImport.JxlExport;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.ctg.socialsecurity.service.CsEmplsocialResultService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 员工社保结果查看
 * @author windy
 * 2009-9-10 上午10:18:56
 */
public class csEmpsocialResultAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private Page result;
	private List<CsEmpsocialResult> empSocialList;
	private CsEmpsocialTotal empSocialTotal;
	
	private CsEmpsocialResultFilter filter;
	
	private CsEmpsocialResultService csEmpsocialResultService; //结果查询
	private CsEmplsocialResultService csEmplsocialResultService; //社保计算
	
	public Page getResult() {
		return result;
	}
	public void setResult(Page result) {
		this.result = result;
	}
	public List<CsEmpsocialResult> getEmpSocialList() {
		return empSocialList;
	}
	public void setEmpSocialList(List<CsEmpsocialResult> empSocialList) {
		this.empSocialList = empSocialList;
	}
	public CsEmpsocialTotal getEmpSocialTotal() {
		return empSocialTotal;
	}
	public void setEmpSocialTotal(CsEmpsocialTotal empSocialTotal) {
		this.empSocialTotal = empSocialTotal;
	}
	
	public void setCsEmpsocialResultService(
			CsEmpsocialResultService csEmpsocialResultService) {
		this.csEmpsocialResultService = csEmpsocialResultService;
	}
	public void setCsEmplsocialResultService(
			CsEmplsocialResultService csEmplsocialResultService) {
		this.csEmplsocialResultService = csEmplsocialResultService;
	}
	
	public CsEmpsocialResultFilter getFilter() {
		return filter;
	}
	public void setFilter(
			CsEmpsocialResultFilter filter) {
		this.filter = filter;
	}
	
	private SysUser getCurrentUser(){
		return (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
	}

	/**
	 * 点击页面查询按钮
	 * @return 翻页查询的结果
	 */
	public String QueryEmpSocialResult() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if(filter == null){
				filter = new CsEmpsocialResultFilter();
			}
			filter.setStartIndex(Parse.toInteger(request.getParameter(Constants.START) , 0));
			filter.setEndIndex(Parse.toInteger(request.getParameter(Constants.LIMIT), Constants.DEFAULT_PAGE_SIZE));
			filter.setCompanyCode(getCurrentUser().getCompanyCode());
			result = csEmpsocialResultService.queryCsEmpResult(filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}	
	/**
	 * 点击明细查看，查看雇员社保明细项
	 * @return
	 */
	public String QueryEmpSocialList() {
		try {
			empSocialList = csEmpsocialResultService.queryCsEmpsocialList(filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/*
	 * 点击社保明细查看, 查看当前社保合计项数据
	 */
	public String QueryEmpSocialTotal(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			
			if (filter==null) {
				filter = new CsEmpsocialResultFilter();
			}
			filter.setStartIndex(Parse.toInteger(request.getParameter(Constants.START) , 0));
			filter.setEndIndex(Parse.toInteger(request.getParameter(Constants.LIMIT), Constants.DEFAULT_PAGE_SIZE));
			
			empSocialTotal = csEmpsocialResultService.queryCsEmpsocialTotal(filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String SocialCalculate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String calcMonth = request.getParameter("CalcMonth").trim(); 
		String calcScope = request.getParameter("CalcScope").trim();
		String companyCode = ((SysUser)ServletActionContext.getRequest()
				.getSession().getAttribute(Constants.CURRENT_USER)).getCompanyCode();
		
		//调用社保计算接口
		try {
			if(calcScope.equals("0")){
				//部分员工
				String ids = request.getParameter("ids");
				String [] employeeIds = ids.split(",");
				List<Integer> addedList = new ArrayList<Integer>();
				for (int i = 0; i < employeeIds.length; i++) {
				   addedList.add(Integer.parseInt(employeeIds[i]));
				}
				
				if(addedList.size() != 0){
					try{
						csEmplsocialResultService.makeEmplsocialResult(addedList, calcMonth,null);
					}catch (Exception e){
						e.printStackTrace();
					}
				}
				runFlag = 1;
			}else{
				//全部员工
				csEmplsocialResultService.makeEmplsocialResult(companyCode, calcMonth,null);
			}
			runFlag = 1;
		} catch (Exception e) {
			runFlag = 0;
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String DownloadData(){
		runFlag = 0;
		List<CsEmpsocialTotal> empSocialTotalList = null;
		
		//获取数据内容
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if(filter == null){
				filter = new CsEmpsocialResultFilter();
			}
			filter.setStartIndex(0);
			filter.setEndIndex(Parse.toInteger(request.getParameter(Constants.LIMIT), Constants.EXPORT_MAX_ROW));
			filter.setCompanyCode(getCurrentUser().getCompanyCode());
			empSocialTotalList = (ArrayList)csEmpsocialResultService.queryCsEmpResult(filter).getResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//获取数据列标题对应对字段
		List<ColumnModel> titles = new ArrayList();
		ColumnModel columnModel = new ColumnModel();
		columnModel.setHeader("年度月份");
		columnModel.setDataIndex("period");
		titles.add(columnModel);
		columnModel = new ColumnModel();
		columnModel.setHeader("雇员编号");
		columnModel.setDataIndex("empCode");
		titles.add(columnModel);
		columnModel = new ColumnModel();
		columnModel.setHeader("雇员姓名");
		columnModel.setDataIndex("name");
		titles.add(columnModel);
		columnModel = new ColumnModel();
		columnModel.setHeader("社保额");
		columnModel.setDataIndex("personTotal");
		titles.add(columnModel);
		columnModel = new ColumnModel();
		columnModel.setHeader("计税额");
		columnModel.setDataIndex("personTaxTotal");
		titles.add(columnModel);
		
		//写excel文件
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String("export.xls".getBytes(), "utf-8"));
			OutputStream output = response.getOutputStream();
			output.flush();
			JxlExport.writeExcel(output, empSocialTotalList, titles);
			output.close();
		} catch (WriteException e) {
			runFlag = 1;
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			runFlag = 2;
			e.printStackTrace();
		} catch (IntrospectionException e) {
			runFlag = 3;
			e.printStackTrace();
		} catch (IOException e) {
			runFlag = 4;
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			runFlag = 5;
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			runFlag = 6;
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	private int runFlag;

	public int getRunFlag() {
		return runFlag;
	}
	public void setRunFlag(int runFlag) {
		this.runFlag = runFlag;
	}
}
